package com.hgz;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hgz.util.OperateTime;
import com.hgz.util.ReadAndWriteFile;

public class Fecth {


	String bb(){
		String url ="http://www.xinfadi.com.cn/do/list_form.php?mid=10";
		String url2 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=2";
		String url3 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=3";
		Document doc;
		try {
			doc = Jsoup.connect(url).get();
			//Elements headContents = doc.select("tr[class=formlist_head]"); 
			Elements trContents  = doc.select("tr[class=formlist_tr]");
			//			 for (Element headContent : headContents) {
			//				 
			//				 System.out.println (headContent.text());
			//		        }
			String filename = OperateTime.getDate()+""+".txt";
			String fileurl="/home/scott/workspace/Guestbook/war/WEB-INF/data/"+filename;
			for (Element trContent : trContents){
				System.out.println (trContent.text());
				String brName = trContent.text();
				String [] names = brName.split(" ");
				for(int i=0;i< names.length;i++){
					Item item = new Item();
					item.setName("土豆");
					item.setMinPrice("1.0");
					item.setPrice("1.5");
					item.setMaxPrice("2.0");
					item.setDw("斤");

				}
				System.out.println(names.length);
				ReadAndWriteFile.javaWriteFile(fileurl,brName);

			}
			ReadAndWriteFile.javaReaderFile(fileurl);
			//			String progectUrl = Thread.currentThread().getContextClassLoader().getResource("").getPath();
			//			
			//			progectUrl.replace("classes", "");
			//			
			//			System.out.println("progectUrl"+progectUrl);




		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "s";
	}

	String bb2(){
		String url ="http://www.xinfadi.com.cn/do/list_form.php?mid=10";
		String url2 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=2";
		String url3 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=3";

		Document doc;
		try {
			doc = Jsoup.connect(url2).get();

			Elements headContents = doc.select("tr[class=formlist_head]"); 
			Elements trContents  = doc.select("tr[class=formlist_tr]");
			for (Element headContent : headContents) {

				System.out.println (headContent.text());
			}

			for (Element trContent : trContents) {
				System.out.println (trContent.text());
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "s";
	}

	String bb3(){
		String url ="http://www.xinfadi.com.cn/do/list_form.php?mid=10";
		String url2 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=2";
		String url3 ="http://www.xinfadi.com.cn/do/list_form.php?mid=10&page=3";

		Document doc;
		try {
			doc = Jsoup.connect(url3).get();

			Elements headContents = doc.select("tr[class=formlist_head]"); 
			Elements trContents  = doc.select("tr[class=formlist_tr]");
			for (Element headContent : headContents) {
				System.out.println (headContent.text());
			}

			for (Element trContent : trContents) {
				System.out.println (trContent.text());
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "s";
	}
	private static String trim(String s, int width) {
		if (s.length() > width)
			return s.substring(0, width-1) + ".";
		else
			return s;
	}

	//	void aa(){
	//		/* 1 生成 HttpClinet 对象并设置参数*/
	//		  DefaultHttpClient httpclient = new DefaultHttpClient();
	//		  
	//		  HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");
	//
	//		   HttpResponse response = httpclient.execute(httpget);
	//           HttpEntity entity = response.getEntity();
	//
	//          
	//          
	//		   
	//	}
	public static void main(String args[]){
		System.out.println(new Fecth().bb());
		//System.out.println(new Fecth().bb2());
		//System.out.println(new Fecth().bb3());

	}

}
